package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
)
import _ "github.com/jinzhu/gorm/dialects/sqlite"

type User struct {
	Name string `gorm:"column:name"`
	Age int `gorm:"column:age"`
	db *gorm.DB
}

func (u *User) Create(nu User)  {
	u.db.Create(&nu)
}

func (u *User) QueryOne(name string ,age int ) (qu User)  {
	u.db.Where("name = ?",name).Where("age = ?",age).Scan(&qu)
	return
}

func (u *User) QueryMany() {
	
}

func ConnDB()  (db *gorm.DB,err error){
	db,err = gorm.Open("sqlite3","./tmp.db")
	if err != nil {
		panic("Database connection failed")
	}
	db.AutoMigrate(&User{})
	return

}

func main()  {
	db ,err := ConnDB()
	if err != nil {
		return
	}
	u := User{
		db:   db,
	}

	u.Create(User{
		Name: "Dong",
		Age:  100,
	})

	qu := u.QueryOne("Dong",1)
	fmt.Println(qu)
}